package com.demo.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.module.commonui.R;

public class SettingItemSwitchView extends SettingItemLineView {
    private String mName;
    @SuppressLint("UseSwitchCompatOrMaterialCode")
    public Switch mSwitch;
    protected View mView;
    private int mTextColor;
    private TextView mTextView;
    private boolean mShow = false;
    private int mNameTextSize = 14;
    private Drawable mItemDrawable;
    private Drawable mTextLeftDrawable;
    private int resource;
    private int mNameType = 0;
    public SettingItemSwitchView(Context context) {
        super(context);
        initContentView(context);
    }

    public SettingItemSwitchView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SettingItemSwitchView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initParam(context, attrs);
        initContentView(context);
    }

    private void initParam(Context context, @Nullable AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SettingItemSwitchView);
        resource = typedArray.getResourceId(R.styleable.SettingItemSwitchView_switch_item_layout, getLayoutResID());
        mName = typedArray.getString(R.styleable.SettingItemSwitchView_switch_text_name);
        mShow = typedArray.getBoolean(R.styleable.SettingItemSwitchView_switch_line_visibility, false);
        mTextColor = typedArray.getColor(R.styleable.SettingItemSwitchView_switch_text_color, context.getResources().getColor(R.color.global_customize_fixed_color_black));
        mNameTextSize = typedArray.getInt(R.styleable.SettingItemSwitchView_switch_text_name_size, mNameTextSize);
        mItemDrawable = typedArray.getDrawable(R.styleable.SettingItemSwitchView_switch_item_background);
        mTextLeftDrawable = typedArray.getDrawable(R.styleable.SettingItemSwitchView_switch_text_drawableLeft);
        mNameType = typedArray.getInt(R.styleable.SettingItemSwitchView_switch_text_name_style, 0);
        typedArray.recycle();
    }

    public int getLayoutResID() {
        return R.layout.settings_item_switch;
    }

    private void initView(Context context){
        if (mView == null){
            mView = LayoutInflater.from(context).inflate(resource, this, true);
        }
    }

    private void initContentView(Context context) {
        initView(context);
        mTextView = mView.findViewById(R.id.tv_item_text_name);
        mTextView.setText(mName);
        mTextView.setTextColor(mTextColor);
        mTextView.setTextSize(mNameTextSize);
        if (mNameType == 1){
            mTextView.setTypeface(null, Typeface.BOLD);
        }
        mSwitch = mView.findViewById(R.id.sw_settings_content);
        mLineView = mView.findViewById(R.id.rl_line);
        if (mItemDrawable != null){
            mView.findViewById(R.id.switch_bg).setBackground(mItemDrawable);
        }

        if (mTextLeftDrawable != null){
            mTextView.setCompoundDrawablesWithIntrinsicBounds(mTextLeftDrawable, null, null, null);
        }

        showLine(mShow);
    }

    public void setOnClickListener(@Nullable OnClickListener l) {
        mSwitch.setOnClickListener(l);
    }
    public void setOnItemClickListener(@Nullable OnClickListener l){
        mView.findViewById(R.id.switch_bg).setOnClickListener(l);
    }
    public void setOnCheckedChangeListener(@Nullable CompoundButton.OnCheckedChangeListener listener){
        mSwitch.setOnCheckedChangeListener(listener);
    }
    public void setChecked(boolean checked) {
        mSwitch.setChecked(checked);
    }

    public boolean isChecked() {
        return mSwitch.isChecked();
    }

    public void setText(String text){
        mTextView.setText(text);
    }
}
